#include "IOConfig.h"
#include "usbd_cdc.h"
#include "ics307.h"
#include "fsmc.h"
#include "time.h"
#include "timer.h"
#include "adc_volt.h"
#include "w25qxx.h"
#include "mtp_pwm.h"
#include "ad5274_5271.h"
#include "iic_current.h"
#include "SPI_PANEL_INIT.H"


void LED_IO_init()
{
    GPIO_InitTypeDef GPIO_Initure;
	__HAL_RCC_GPIOC_CLK_ENABLE();			//使能GPIOC时钟	
	__HAL_RCC_GPIOG_CLK_ENABLE();			//使能GPIOG时钟	
	
	GPIO_Initure.Speed= GPIO_SPEED_HIGH;     //注意管脚变更时对应的端口时钟
	GPIO_Initure.Pull =GPIO_NOPULL;
	GPIO_Initure.Mode = GPIO_MODE_OUTPUT_PP;	
	GPIO_Initure.Pin = LED1|LED2;				
	HAL_GPIO_Init(LED1Periph_GPIO,&GPIO_Initure); 

	GPIO_Initure.Speed= GPIO_SPEED_HIGH;     //注意管脚变更时对应的端口时钟
	GPIO_Initure.Pull =GPIO_NOPULL;
	GPIO_Initure.Mode = GPIO_MODE_OUTPUT_PP;	
	GPIO_Initure.Pin =LED3|LED4|LED5;				
	HAL_GPIO_Init(LED3Periph_GPIO,&GPIO_Initure); 	
	LED3_HIGH;
}


void TEST_MODE0_init()
{
    GPIO_InitTypeDef GPIO_Initure;
	__HAL_RCC_GPIOD_CLK_ENABLE();			//使能GPIOD时钟	
	GPIO_Initure.Pin = MIPI_DPHY_CPHY_SEL;	//GPIO_D3 ,DPHY/CPHY 选择
	GPIO_Initure.Speed= GPIO_SPEED_HIGH;     
	GPIO_Initure.Pull =GPIO_PULLUP;
	GPIO_Initure.Mode = GPIO_MODE_OUTPUT_PP;				
	HAL_GPIO_Init(MIPI_DPHY_CPHY_SEL_GPIO,&GPIO_Initure);
    TEST_MODE0_DPHY;  //默认选择DPHY
}


void IF_Sel_init()
{
    GPIO_InitTypeDef GPIO_Initure;
	__HAL_RCC_GPIOG_CLK_ENABLE();			//使能GPIOG时钟	
	
	GPIO_Initure.Pin = IF_Sel1;	
	GPIO_Initure.Speed= GPIO_SPEED_HIGH;     
	GPIO_Initure.Pull =GPIO_PULLUP;
	GPIO_Initure.Mode = GPIO_MODE_OUTPUT_PP;				
	HAL_GPIO_Init(IF_SelPeriph_GPIO,&GPIO_Initure); 		
		
    IF_Sel1_Video;
}

void FPGA_CMD_LOGIC_init(void)
{
	GPIO_InitTypeDef GPIO_Initure;
	__HAL_RCC_GPIOE_CLK_ENABLE();			//使能GPIOE时钟	
	GPIO_Initure.Pin = FPGA_CMD_LOGIC;	   
	GPIO_Initure.Speed= GPIO_SPEED_HIGH;     
	GPIO_Initure.Pull = GPIO_PULLDOWN;
	GPIO_Initure.Mode = GPIO_MODE_OUTPUT_PP;	
	HAL_GPIO_Init(FPGA_CMD_LOGICPeriph_GPIO,&GPIO_Initure); 	
	MCU_LOGIC_EN_HIGH;  //初始状态，默认高电平
}

u8 FPGA_ID_Gain(void)
{
	u8 id;
	GPIO_InitTypeDef GPIO_Initure;
	__HAL_RCC_GPIOE_CLK_ENABLE();			//使能GPIOE时钟	
	GPIO_Initure.Pin = FPGA_ID0;	   //输入状态/区分FPGA型号，1->A7100T , 0->SPARTAN 6
	GPIO_Initure.Speed= GPIO_SPEED_HIGH;     
	GPIO_Initure.Pull = GPIO_NOPULL;
	GPIO_Initure.Mode = GPIO_MODE_INPUT;					
	HAL_GPIO_Init(FPGA_IDPeriph_GPIO,&GPIO_Initure); 	

	id = HAL_GPIO_ReadPin(FPGA_IDPeriph_GPIO,FPGA_ID0);
	return id;
}

void BUTTON_IO_init()
{
    GPIO_InitTypeDef GPIO_Initure;
	__HAL_RCC_GPIOD_CLK_ENABLE();			//使能GPIOD时钟	
	__HAL_RCC_GPIOE_CLK_ENABLE();			//使能GPIOE时钟	
	__HAL_RCC_GPIOB_CLK_ENABLE();			//使能GPIOB时钟	
	__HAL_RCC_GPIOA_CLK_ENABLE();			//使能GPIOA时钟
	
	GPIO_Initure.Speed= GPIO_SPEED_HIGH;     
	GPIO_Initure.Pull =GPIO_PULLUP;
	GPIO_Initure.Mode = GPIO_MODE_INPUT;	
	GPIO_Initure.Pin = KEY_DOWN;				
	HAL_GPIO_Init(KEY_DOWNPeriph_GPIO,&GPIO_Initure); 	//DOWN	
		
	GPIO_Initure.Speed= GPIO_SPEED_HIGH;     
	GPIO_Initure.Pull =GPIO_PULLUP;
	GPIO_Initure.Mode = GPIO_MODE_INPUT;	
	GPIO_Initure.Pin = KEY_UP;				
	HAL_GPIO_Init(KEY_UPPeriph_GPIO,&GPIO_Initure); 	
	
	GPIO_Initure.Speed= GPIO_SPEED_HIGH;     
	GPIO_Initure.Pull =GPIO_PULLUP;
	GPIO_Initure.Mode = GPIO_MODE_INPUT;	
	GPIO_Initure.Pin = KEY_OK;				
	HAL_GPIO_Init(KEY_OKPeriph_GPIO,&GPIO_Initure); 	//OK	
	
	GPIO_Initure.Speed= GPIO_SPEED_HIGH;     
	GPIO_Initure.Pull =GPIO_PULLUP;
	GPIO_Initure.Mode = GPIO_MODE_INPUT;	
	GPIO_Initure.Pin = KEY_DEBUG;				
	HAL_GPIO_Init(KEY_DEBUGPeriph_GPIO,&GPIO_Initure); 	//Debug模式检测
    
}

void EXTIInit(void)
{
    GPIO_InitTypeDef GPIO_Initure;
	
    __HAL_RCC_GPIOA_CLK_ENABLE();               //开启GPI0F时钟
    __HAL_RCC_GPIOD_CLK_ENABLE();               //开启GPI0E时钟
							  
 // RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
    GPIO_Initure.Pin=KEY_DOWN;                //
    GPIO_Initure.Mode=GPIO_MODE_IT_FALLING;      //下降沿触发
    GPIO_Initure.Pull=GPIO_PULLUP;			//上拉
    HAL_GPIO_Init(KEY_DOWNPeriph_GPIO,&GPIO_Initure);  
	
    GPIO_Initure.Pin=KEY_UP;                //
    GPIO_Initure.Mode=GPIO_MODE_IT_FALLING;      //下降沿触发
    GPIO_Initure.Pull=GPIO_PULLUP;			//上拉
    HAL_GPIO_Init(KEY_UPPeriph_GPIO,&GPIO_Initure);  
		
  /* Enable and set EXTI Line0 Interrupt to the lowest priority */
    //中断线8
    HAL_NVIC_SetPriority(EXTI15_10_IRQn,2,0);       //抢占优先级为2，子优先级为0
    HAL_NVIC_SetPriority(EXTI9_5_IRQn,2,1);       //抢占优先级为2，子优先级为1

}

void EXIT_Enable(u8 status)
{
	if(status == Enable) 
	{
		HAL_NVIC_EnableIRQ(EXTI15_10_IRQn);             //使能中断线10  
		HAL_NVIC_EnableIRQ(EXTI9_5_IRQn);             //使能中断线2
	}
	else
	{
		HAL_NVIC_DisableIRQ(EXTI15_10_IRQn);
		HAL_NVIC_DisableIRQ(EXTI9_5_IRQn);             //使能中断线2	
	}
}

void EXTI15_10_IRQHandler(void)
{
	//if(KEY0==0)
    HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_15);  //
	//if(KEY0==1)	
	//HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_9);  //
}
void EXTI9_5_IRQHandler(void)
{
    HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_6);  //
}

void Other_IO_init(void)
{
	GPIO_InitTypeDef GPIO_Initure;  
	
	__HAL_RCC_GPIOA_CLK_ENABLE();               //开启GPI0A时钟	
	__HAL_RCC_GPIOC_CLK_ENABLE();               //开启GPI0C时钟
	__HAL_RCC_GPIOF_CLK_ENABLE();               //开启GPI0F时钟	
	__HAL_RCC_GPIOG_CLK_ENABLE();               //开启GPI0G时钟	
	__HAL_RCC_GPIOB_CLK_ENABLE();               //开启GPI0B时钟
		
	GPIO_Initure.Speed= GPIO_SPEED_HIGH;     //控制电平转换芯片转压芯片(TXS0108EPWR/U45)使能
	GPIO_Initure.Pull =GPIO_PULLUP;
	GPIO_Initure.Mode = GPIO_MODE_OUTPUT_PP;	
	GPIO_Initure.Pin = TXS0108_EN;				
	HAL_GPIO_Init(TXS0108_ENPeriph_GPIO,&GPIO_Initure); 
	TXS0108_EN_HIGH;	//QSPI 接口 电平转换芯片 使能
	
	GPIO_Initure.Speed= GPIO_SPEED_HIGH;     
	GPIO_Initure.Pull =GPIO_PULLDOWN;
	GPIO_Initure.Mode = GPIO_MODE_OUTPUT_PP;	
	GPIO_Initure.Pin = Load_done;			//BMP_FLAG 图片加载信号	
	HAL_GPIO_Init(Load_donePeriph_GPIO,&GPIO_Initure); 	 
	
	GPIO_Initure.Speed= GPIO_SPEED_HIGH;     
	GPIO_Initure.Pull =GPIO_PULLDOWN;
	GPIO_Initure.Mode = GPIO_MODE_OUTPUT_PP;	
	GPIO_Initure.Pin = FPGA_Fifo0Reset;		//		
	HAL_GPIO_Init(FPGA_Fifo0ResetPeriph_GPIO,&GPIO_Initure); 	 
	
//	GPIO_Initure.Speed= GPIO_SPEED_HIGH;     
//	GPIO_Initure.Pull =GPIO_PULLDOWN;
//	GPIO_Initure.Mode = GPIO_MODE_OUTPUT_PP;	
//	GPIO_Initure.Pin = FPGA_Fifo0Wen;				
//	HAL_GPIO_Init(FPGA_Fifo0WenPeriph_GPIO,&GPIO_Initure); 	 

	GPIO_Initure.Speed= GPIO_SPEED_HIGH;     
	GPIO_Initure.Pull =GPIO_PULLDOWN;
	GPIO_Initure.Mode = GPIO_MODE_OUTPUT_PP;	
	GPIO_Initure.Pin = FPGA_Reset;		//FPGA_Reset		
	HAL_GPIO_Init(FPGA_ResetPeriph_GPIO,&GPIO_Initure); 
	
	GPIO_Initure.Speed= GPIO_SPEED_HIGH;     
	GPIO_Initure.Pull =GPIO_PULLUP;
	GPIO_Initure.Mode = GPIO_MODE_OUTPUT_PP;	
	GPIO_Initure.Pin = SSD_RESET;			//SSD2832 Reset	
	HAL_GPIO_Init(SSD_RESETPeriph_GPIO,&GPIO_Initure); 

	GPIO_Initure.Speed= GPIO_SPEED_HIGH;     
	GPIO_Initure.Pull =GPIO_PULLUP;
	GPIO_Initure.Mode = GPIO_MODE_OUTPUT_PP;	
	GPIO_Initure.Pin = OLED_RESET;				
	HAL_GPIO_Init(OLED_ResetPeriph_GPIO,&GPIO_Initure); 
	
	GPIO_Initure.Speed= GPIO_SPEED_LOW;     
	GPIO_Initure.Pull =GPIO_NOPULL;
	GPIO_Initure.Mode = GPIO_MODE_OUTPUT_PP;	
	GPIO_Initure.Pin = USB_RESET;				
	HAL_GPIO_Init(USB_RESETPeriph_GPIO,&GPIO_Initure); 
	
	GPIO_Initure.Speed= GPIO_SPEED_HIGH;     
	GPIO_Initure.Pull =GPIO_PULLUP;
	GPIO_Initure.Mode = GPIO_MODE_OUTPUT_PP;	
	GPIO_Initure.Pin = FDEN;				
	HAL_GPIO_Init(FD_ENPeriph_GPIO,&GPIO_Initure); 	
	FD_EN_HIGH;		//OLED Power Enable
	
	GPIO_Initure.Speed= GPIO_SPEED_HIGH;     
	GPIO_Initure.Pull =GPIO_PULLUP;
	GPIO_Initure.Mode = GPIO_MODE_OUTPUT_PP;	
	GPIO_Initure.Pin = RT8082_EN;				
	HAL_GPIO_Init(RT8082_ENPeriph_GPIO,&GPIO_Initure); 	
	RT8082_EN_HIGH;	//VBAT 电压控制
	
	GPIO_Initure.Speed= GPIO_SPEED_HIGH;     
	GPIO_Initure.Pull =GPIO_PULLDOWN;
	GPIO_Initure.Mode = GPIO_MODE_OUTPUT_PP;	
	GPIO_Initure.Pin = SHUTSSD2828;				
	HAL_GPIO_Init(SHUTSSD2828Periph_GPIO,&GPIO_Initure); 	
	SHUTSSD2828_LOW;	//SSD2828 SHUT
}
void FPGA_ResetProcess(void)
{
    FPGA_RESET_LOW;			//FPGA复位
    //USB_RESET_HIGH;
    delay_ms(20);
    FPGA_RESET_HIGH;
    //USB_RESET_LOW;
    delay_ms(5);	
}
void HardWare_Init(void)
{   
	ICS307_IO_init();		//ICS307引脚初始化
	ICS307_Valid_Data = calculate_idt307_config(80);	
	ICS307_ValidClk_Set(ICS307_Valid_Data);
	Other_IO_init();		//VBAT电源控制，FPGA复位引脚，图片载入完成标志引脚，模组复位引脚(SSD2832复位)引脚 初始化

	SPI1_IO_init();			//SPI1接口的引脚初始化
	SPI_PANEL_IO_init();	//PANEL SPI通信接口初始化
	BUTTON_IO_init();		//按键引脚初始化
	TEST_MODE0_init();  //C-PHY&D-PHY
	IF_Sel_init();
	
	FPGA_ID_Gain();
	FPGA_CMD_LOGIC_init();
	LED_IO_init();			//LED引脚初始化
	FSMC_IO_init();	
	W25QXX_Init();		    //W25QXX初始化
	//TIM12_PWM_Init(500-1,108-1);     //PWM频率1M/500=2kHZ
	//NAND_Init();

	TIM3_Init(10-1,10800-1); //定时器3初始化，定时器时钟为108M。分频系数为10800-1	 
	//所以定时器3的频率为108M/10800=10K,自动重装载为10-1，那么定时器周期就是1ms
	ADC_Init();
	T226_Init();
	OTP_EN_IO_init();
	AD5272_GPIO_Config();
	AD5274_5271_GPIO_Config();
	VDD_AD5274_5271_Init(0x0120);
	VDDIO_AD5274_5271_Init(0x140);		
	delay_ms(150);	
	//otp power test	
	VPP_POWER_Init(0.0,OTP_Disable);	
}
